﻿<%@ Page Title="" Language="C#" MasterPageFile="~/PainelUsuario.master" AutoEventWireup="true" CodeFile="relatorio_espacovip.aspx.cs" Inherits="relatorio_espacovip" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <% (this.Master as PainelUsuario).ActiveMenu = 8; %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="TitleContent" Runat="Server">
    Espaço Vip
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentContent" Runat="Server">
        <%
            PL.DataBase.DAO.Instance.Setup(PL.DataBase.DatabaseTypes.MySql, PL.General.Instance.EspacoVipConnectionString);
            PL.DataBase.DAO.Instance.Connector.Open();
            
            string sql = "";
            sql += " SELECT COUNT(Id_LoginLog) AS h00, ";
            for (int i = 1; i < 24; ++i)
            {
                string index = "" + i;
                if (index.Length == 1)
                {
                    index = "0" + index;
                }
                sql += "(SELECT COUNT(Id_LoginLog) FROM tb_LoginLog WHERE LoginDate LIKE '% " + index + "%') AS " + ("h" + index) + ",";                                
            }
            sql += "(SELECT tb_LoginLog.WeekDay FROM tb_LoginLog GROUP BY tb_LoginLog.WeekDay ORDER BY COUNT(*) DESC LIMIT 1) AS peakDay ";
            //sql = sql.Substring(0, sql.Length - 1);              
            sql += " FROM tb_LoginLog WHERE LoginDate LIKE '% 00%' ";
            
            var reader = PL.DataBase.DAO.Instance.Connector.QueryWithReader(sql);
            List<string> hours = new List<string>();
            int highest = 0;
            int lowest = int.MaxValue;
            int total = 0;
            string peak = "Não existem horários de pico!";
            string peakDay = "Não existem dias de pico!";
            if (reader.Read() == true)
            {
                peakDay = "" + reader["peakDay"];
                for (int i = 0; i < 24; ++i)                    
                {
                    string index = "" + i;
                    if (index.Length == 1)
                    {
                        index = "0" + index;
                    }                    
                    index = "h" + index;
                    string buffer = "" + reader[index];
                    hours.Add(buffer);

                    int number = int.Parse(buffer);
                    total += number;
                    if (number > highest)
                    {
                        highest = number;
                        if (i == 1)
                        {
                            peak = "O horário de pico é a <b>" + i + " hora</b> totalizando <b>" + buffer + " acessos</b>";
                        }
                        else
                        {
                            peak = "O horário de pico é as <b>" + i + " horas</b> totalizando <b>" + buffer + " acessos</b>";
                        }
                    }
                    else if (number == highest)
                    {
                        peak += "<br />";
                        if (i == 1)
                        {
                            peak = "O horário de pico é a <b>" + i + " hora</b> totalizando <b>" + buffer + " acessos</b>";
                        }
                        else
                        {
                            peak = "O horário de pico é as <b>" + i + " horas</b> totalizando <b>" + buffer + " acessos</b>";
                        }
                    }
                    
                    if (number < lowest)
                    {
                        lowest = number;
                    }
                }
            }
            reader.Close();
            PL.DataBase.DAO.Instance.Connector.Close();
                        
        %>
            
        <div id="chart_div" style="width: 900px; height: 500px; margin-left: auto; margin-right: auto;"></div>    

        <div style="margin: 15px;">
            <center>                
                Acessos por hora (vitalício):
                <br />
                <br />
                <%
                    List<string> chartsData = new List<string>();
                    for (int i = 0; i < 24; ++i)
                    {                        
                        if (i == 1)
                        {
                            chartsData.Add("['" + i + " hora', " + hours[i] + "]");
                            this.Response.Write("<b>" + i + " hora</b>: <span style='" + this.GetColor(lowest, highest, int.Parse("" + hours[i])) + ";'>" + hours[i] + " acessos</span>");
                        }
                        else
                        {
                            chartsData.Add("['" + i + " horas', " + hours[i] + "]");
                            this.Response.Write("<b>" + i + " horas</b>: <span style='" + this.GetColor(lowest, highest, int.Parse("" + hours[i])) + ";'>" + hours[i] + " acessos</span>");
                        }
                        this.Response.Write("<br />");
                    }                    
                %>

                <br />
                <br />
                Total: <b><%= total %></b> acessos
                <br />
                <%= peak %>
                <br />                
                O dia da semana com mais acessos é <b><%= peakDay %></b>.
            </center>
        </div>

        <br />        

        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("visualization", "1", { packages: ["corechart"] });
            google.setOnLoadCallback(drawChart);
            function drawChart() {
                var data = google.visualization.arrayToDataTable([
                ['Horário', 'Acessos'],
                <%
                    int count = 0;
                    foreach (string data in chartsData)
                    {
                        this.Response.Write(data);
                        if (count < chartsData.Count)
                        {
                            this.Response.Write(",");
                        }
                        ++count;
                    }
                %>                
            ]);

                var options = {
                    title: 'Acessos ao Espaço VIP'                    
                };

                var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
                chart.draw(data, options);
            }
        </script>        
</asp:Content>


